package com.example.a022.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2018/8/22.
 */
public class CommodityDetailAdapter extends BaseAdapter {
    private Context context;
    private List<String> imgs = null;
    private LayoutInflater layoutInflater;

    public CommodityDetailAdapter(Context context, List<String> imgs){
        this.context = context;
        this.imgs = imgs;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return imgs.size();
    }

    @Override
    public Object getItem(int i) {
        return imgs.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view==null){
            viewHolder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.goods_details_item,null);
            viewHolder.imageView = (ImageView) view.findViewById(R.id.img);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        String url = imgs.get(i);
        Log.e("ttt","url"+url);
        final ViewHolder finalViewHolder = viewHolder;
        Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                int width = resource.getWidth();
                int height = resource.getHeight();
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                int widths = wm.getDefaultDisplay().getWidth();
                int heights = wm.getDefaultDisplay().getHeight();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) finalViewHolder.imageView.getLayoutParams();
                params.height = widths*height/width;
                finalViewHolder.imageView.setLayoutParams(params);
            }
        });

       Glide.with(context).load(url).centerCrop().placeholder(R.mipmap.ic_launcher).into(viewHolder.imageView);

        return view;
    }

    private final class ViewHolder{
        ImageView imageView;
    }
}
